package com.zengjin.controller;


import com.google.common.base.CaseFormat;
import com.zengjin.config.Db;
import com.zengjin.config.TableClass;
import com.zengjin.config.ColumnClass;
import com.zengjin.test.common.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zengjin.utils.DBUtils;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 19324
 */
@RestController
@RequestMapping("/db")
public class DbController {
    @PostMapping("/connect")
    public R connect(@RequestBody Db db) {
        System.out.println(db);
        Connection con = DBUtils.initDb(db);
        if (con != null) {
            return R.success("数据库连接成功");
        }
        return R.error("数据库连接失败");
    }

    @PostMapping("/getDBList")
    public R config(@RequestBody Map<String, String> map) {
        String packageName = map.get("packageName");
        try {
            Connection connection = DBUtils.getConnection();
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet tables = metaData.getTables(connection.getCatalog(), null, null, null);
            List<TableClass> tableClassList = new ArrayList<>();
            while (tables.next()) {
                TableClass tableClass = new TableClass();
                tableClass.setPackageName(packageName);
                String tableName = tables.getString("TABLE_NAME");
                String enName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName);
                tableClass.setTableName(tableName);
                tableClass.setEnName(enName);
                tableClass.setControllerName(enName + "Controller");
                tableClass.setMapperName(enName + "Mapper");
                tableClass.setServiceName(enName+"Service");
                tableClass.setVoName(enName + "Vo");
                tableClass.setDtoName(enName + "Dto");

                // 获取表的字段信息
                ResultSet columns = metaData.getColumns(connection.getCatalog(), null, tableName, null);
                List<ColumnClass> columnList = new ArrayList<>();
                while (columns.next()) {
                    ColumnClass columnClass = new ColumnClass();
                    String columnName = columns.getString("COLUMN_NAME");
                    String propertyName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnName);
                    String type = columns.getString("TYPE_NAME");
                    String remark = columns.getString("REMARKS");

                    columnClass.setPropertyName(propertyName);
                    columnClass.setColumnName(columnName);
                    columnClass.setType(type);
                    columnClass.setRemark(remark);
                    columnClass.setIsVo(false);
                    columnClass.setIsDto(false);

                    // 判断是否为主键
                    ResultSet primaryKeys = metaData.getPrimaryKeys(connection.getCatalog(), null, tableName);
                    boolean isPrimary = false;
                    while (primaryKeys.next()) {
                        if (columnName.equals(primaryKeys.getString("COLUMN_NAME"))) {
                            isPrimary = true;
                            break;
                        }
                    }
                    columnClass.setPrimary(isPrimary);

                    columnList.add(columnClass);
                }
                tableClass.setColumns(columnList);

                tableClassList.add(tableClass);
            }
            return R.success(tableClassList,"数据库信息读取成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error("数据库信息读取失败");
    }
}
